package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.OrderInfo;
import com.theo.entity.StockInfo;
import com.theo.mapper.StockInfoMapper;
import com.theo.service.OrderInfoService;
import com.theo.service.StockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-03-31
 */
@Controller
@RequestMapping("/order-info")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private StockInfoService stockInfoService;
    @Autowired
    private StockInfoMapper stockInfoMapper;

    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<OrderInfo> list = orderInfoService.queryAllOrderInfo();
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/order_list";
    }

    @RequestMapping("/saveMask.do")
    public String save(OrderInfo orderInfo) {
        orderInfoService.saveMaskOrderInfo(orderInfo);
        StockInfo stockInfo = stockInfoService.queryStockInfoByArticleId(orderInfo.getArticleId());
        stockInfo.setStockNum(stockInfo.getStockNum()-orderInfo.getOrderNum());
        stockInfoService.updateSotckInfo(stockInfo);
        return "redirect:/hospital-info/show";

    }
    @RequestMapping("/delete.do")
    public String delete(Integer id) {
        orderInfoService.deleteOrderInfo(id);
        return "redirect:/order-info/show.do";
    }

    @RequestMapping("/toUpdate.do")
    public String toUpdate() {
        return "admin/order_update";
    }

    @RequestMapping("/update.do")
    public String update(OrderInfo orderInfo) {
        orderInfoService.updateOrderInfo(orderInfo);
        return "redirect:/order-info/show.do";
    }
}

